﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using PGun.WindowsInput;

namespace HangameAuto02
{
    public static class Common
    {
        public static IntPtr ptrHanGame;
        public static GameKind gkGameKind;
        public static bool bCancel = false;

        public static void  SetGameProcess()
        {
            ptrHanGame = IntPtr.Zero;
            Process[] process01 = Process.GetProcessesByName("Baduki");
            if (process01.Length > 0)
            {
                gkGameKind = GameKind.Baduk;
                process01[0].Refresh();
                ptrHanGame = process01[0].MainWindowHandle;
            }

            process01 = Process.GetProcessesByName("Highlow2");
            if (process01.Length > 0)
            {
                gkGameKind = GameKind.Highlow;
                process01[0].Refresh();
                ptrHanGame = process01[0].MainWindowHandle;
            }

            process01 = Process.GetProcessesByName("Poker7");
            if (process01.Length > 0)
            {
                gkGameKind = GameKind.Poker7;
                process01[0].Refresh();
                ptrHanGame = process01[0].MainWindowHandle;
            }
        }


        public  static Bitmap GetHangameImage()
        {
            Bitmap screenshot;
            RECT rect = new RECT();
            NativeMethods.SetForegroundWindow(ptrHanGame);
            if (NativeMethods.GetWindowRect(ptrHanGame, ref rect))
            {
                Size size = new Size(rect.Right - rect.Left,
                                     rect.Bottom - rect.Top);
                using (Graphics browserGraphics = Graphics.FromHwnd(ptrHanGame))
                {
                    screenshot = new Bitmap(size.Width, size.Height,browserGraphics);
                    using (Graphics imageGraphics = Graphics.FromImage(screenshot))
                    {
                        int hWndX = rect.Left;
                        int hWndY = rect.Top;

                        imageGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        imageGraphics.SmoothingMode = SmoothingMode.HighQuality;
                        imageGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        imageGraphics.CompositingQuality = CompositingQuality.HighQuality;
                        imageGraphics.CopyFromScreen(hWndX, hWndY, 0, 0, size);
                        return screenshot;
                    }

                }
            }

            return null;
        }

        public static double[] GetInputPoint(int pX, int pY)
        {
            return new double[] { Convert.ToDouble(pX) / Screen.PrimaryScreen.Bounds.Width * 65535, Convert.ToDouble(pY) / Screen.PrimaryScreen.Bounds.Height * 65535 };
        }

        public static double[] GetInputPoint(Point pPoint)
        {
            return new double[] { Convert.ToDouble(pPoint.X) / Screen.PrimaryScreen.Bounds.Width * 65535, Convert.ToDouble(pPoint.Y) / Screen.PrimaryScreen.Bounds.Height * 65535 };
        }

        public static void AutoChannelIn(double[] pRefresh, double[] pRoomIn, double[] pCurrent, int[] pCompare,
                                         RECT pRect, Color pColor)
        {
            InputSimulator input = new InputSimulator();

            bool bIn = false;
            while (!bIn)
            {

                input.Mouse.MoveMouseTo(pRefresh[0], pRefresh[1]);
                input.Mouse.LeftButtonClick();
                Thread.Sleep(200);

                input.Mouse.MoveMouseTo(pRoomIn[0], pRoomIn[1]);
                input.Mouse.LeftButtonClick();

                Thread.Sleep(1300);
                Color color2 = GetHangameImage().GetPixel(pRect.Left + pCompare[0], pRect.Top + pCompare[1]);
                if (pColor != color2)
                    bIn = true;
            }
            //input.Mouse.MoveMouseTo(pCurrent[0], pCurrent[1]);
        }

        public static void AutoClick(Point pPoint)
        {
            InputSimulator input = new InputSimulator();
            double[] dPoint = GetInputPoint(pPoint);
            while (true)
            {
                input.Mouse.MoveMouseTo(dPoint[0], dPoint[1]);
                input.Mouse.LeftButtonDown();
                Thread.Sleep(500);
                input.Mouse.LeftButtonUp();
                Thread.Sleep(500);
            }
        }
    }
}
